package bb.lanxing.activity.bluetooth;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.model.database.Device;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.view.BiciAlertDialogBuilder;

public class X1Activity extends BaseActivity {
    private Button connectStateBtn;
    private Device device;
    private ImageButton deviceImageView;
    private Button deviceNameBtn;
    private TextView deviceNameView;
    private View mConnectStateBtn;
    private View mDeviceSettingBtn;

    private void registerReceiver() {
    }

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_x1);
        bindView(getWindow().getDecorView());
        setupActionBar(true);
        Device connectedDevice = Device.getConnectedDevice(5);
        this.device = connectedDevice;
        if (connectedDevice == null) {
            finish();
            return;
        }
        setTitle("X1码表");
        String name = this.device.getName();
        this.deviceNameView.setText(name);
        this.deviceNameBtn.setText(name);
        this.deviceImageView.setImageResource(R.drawable.ic_default);
        registerReceiver();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void disconnectBtnClick() {
        new BiciAlertDialogBuilder(this)
                .setTitle("断开蓝牙")
                .setMessage("断开蓝牙后将不会再记住此设备，确定要断开吗?")
                .setPositiveButton("断开", (dialogInterface, i) -> disconnect())
                .setNegativeButton("取消", (dialogInterface, i) -> {
                }).show();
    }

    private void deviceSettingClick() {
        //TODO:hu
//        startActivity(new Intent(this, X1SettingActivity.class));
    }

    public void disconnect() {
        if (this.device.getDeviceNumber() > 0) {
            MyHttpClient.deleteDevice(this.device.getDeviceNumber());
        }
        Device.deleteAll(Device.class, "type = ? and user_id = ?", String.valueOf(5), String.valueOf(App.getContext().getUserId()));
        App.getContext().disconnectDevice(this.device.getType());
        finish();
    }

    private void bindView(View view) {
        this.deviceNameView = (TextView) view.findViewById(R.id.deviceNameView);
        this.deviceNameBtn = (Button) view.findViewById(R.id.deviceNameBtn);
        this.connectStateBtn = (Button) view.findViewById(R.id.connectStateBtn);
        this.deviceImageView = (ImageButton) view.findViewById(R.id.deviceImageView);
//        this.mConnectStateBtn = view.findViewById(R.id.connectStateBtn);
        this.mDeviceSettingBtn = view.findViewById(R.id.deviceSettingBtn);
        this.mConnectStateBtn.setOnClickListener(v -> disconnectBtnClick());
        this.mDeviceSettingBtn.setOnClickListener(v -> deviceSettingClick());
    }

}
